﻿using System;
using System.Configuration;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using FrontDisplay.Controls;

namespace FrontDisplay
{
    /// <summary>
    /// Interaction logic for InfoPage.xaml
    /// </summary>
    public partial class InfoPage : UserControl, IPlugin
    {
        private static readonly InfoPage _instance = new InfoPage();
        private Hotkey _hotkey;
        private readonly StackPanel _menuButton;
        private readonly OutlineTextBlock _timeText;
        private readonly OutlineTextBlock _dateText;

        internal InfoPage()
        {
            InitializeComponent();

            _menuButton = new StackPanel { HorizontalAlignment = HorizontalAlignment.Center };
            _timeText = new OutlineTextBlock { FontSize = 90, HorizontalAlignment = HorizontalAlignment.Center };
            _dateText = new OutlineTextBlock { FontSize = 70, HorizontalAlignment = HorizontalAlignment.Center };

            _menuButton.Children.Add(_timeText);
            _menuButton.Children.Add(_dateText);

            MouseLeftButtonDown += delegate { Navigation.Navigate(MenuPage.Instance); };
            UpdateTime();
        }

        public void UpdateTime()
        {
            _timeText.Text = DateTime.Now.ToString("HH:mm");
            _dateText.Text = DateTime.Now.ToString("d.MM");
        }

        public static InfoPage Instance
        {
            get { return _instance; }
        }

        public void ShowCenterInfo(UIElement control)
        {
            if (control != null)
            {
                CenterPanel.Child = control;
            }
        }

        public void ShowTopInfo(UIElement control)
        {
            ShowInfoControl(control, TopPanel);
        }

        public void ShowBottomInfo(UIElement control)
        {
            ShowInfoControl(control, BottomPanel);
        }

        private static void ShowInfoControl(UIElement control, Panel panel)
        {
            if (control != null)
            {
                ThemeManager.UpdateControlForeground(control);
                foreach (UIElement item in panel.Children)
                {
                    item.Visibility = Visibility.Collapsed;
                }
                if (!panel.Children.Contains(control))
                {
                    panel.Children.Add(control);
                }
                control.Visibility = Visibility.Visible;
            }
        }

        #region IPlugin interface

        public event Action<IPlugin> Activated;
        public event Action<IPlugin> Deactivated;

        public bool IsActive
        {
            get { return true; }
        }

        public string Title
        {
            get { return Def.Localization.GetString("Global.Info"); }
        }

        public UIElement MenuButton
        {
            get { return _menuButton; }
        }

        public void Start()
        {
            //TODO: move into configuration.
            _hotkey = new Hotkey(new HotkeyInfo {Win = true, Shift = true, Key = Key.I}, () => Navigation.Navigate(PageControl));
            _hotkey.Register(Application.Current.MainWindow);
            UpdateTime();
        }

        public void Stop()
        {
            if (_hotkey != null) _hotkey.Unregister();
        }

        public void ThemeChanged() { }

        public Control PageControl
        {
            get { return Instance; }
        }

        public UIElement TopControl { get { return null; } }

        public UIElement BottomControl { get { return null; } }

        public UIElement ParametersControl { get { return null; } }
        
        public ApplicationSettingsBase Config { get { return null; } }

        public void SaveConfig(UIElement element) { }

        #endregion

    }
}
